Creating Data Tables with World Bank Data

Utilizing the World Bank API, I was able to pull country-level data for a variety of variables like income, life expectancy, etc. and present them in the following interactive data table. As I’m interested in Nepal as the site of my summer global independent study, I will utilize that country’s data for the following examples.

library(WDI)
library(ggplot2)
library(plotly)
library(dplyr)
library(DT)

WorldBankData<- WDI(country = c("US","NPL"),
                    indicator=c("SI.POV.GINI", # Gini
                                "NY.GDP.PCAP.CD", # GDP
                                "SP.DYN.LE00.IN", # life expectancy
                                "SP.POP.TOTL", # population
                                "SN.ITK.DEFC.ZS"), # undernourishment
                    start = 1990,
                    end=2017,
                    extra = TRUE)
save(WorldBankData, file = "WorldBankData.RData")

WorldBankData <- dplyr::rename(WorldBankData, 
                        GDP = NY.GDP.PCAP.CD,
                        life_expectancy = SP.DYN.LE00.IN, 
                        population = SP.POP.TOTL,
                        Gini = SI.POV.GINI,
                        undernourishment = SN.ITK.DEFC.ZS)

WorldBankData$country_name <- WorldBankData$country
WorldBankData$country <- as.factor(WorldBankData$country)
save(WorldBankData, file = "WorldBankData.RData")

WorldBankData %>% 
  select(country, 
         region,
         year,
         GDP, 
         life_expectancy, 
         population, 
         Gini,
         undernourishment) %>%
  datatable(rownames = FALSE,
            filter = 'top', 
            extensions = 'Buttons', 
            options = list(
              dom = 'frtipB',
              buttons = c('copy', 'csv', 'excel', 'pdf', 'print')))

Simple Graph of Life Expectancy by Country

That data is then translated into a simple line graph with the ggplot2 package.

ggplot(WorldBankData,
       aes(x=year,
           y=life_expectancy,
           color=country,
           frame=year))+
  geom_line(size=.9)+
  labs(title = "Life Expectancy Over Time",
       x="Year",
       y="Life Expectancy")+
theme(plot.title = element_text(hjust = 0.5, face = "bold", size = 14))+
theme(axis.title.x = element_text(hjust=0.5, face = "bold"))+
  theme(axis.title.y = element_text(face = "bold"))+
  theme(legend.position = "bottom")+
  theme(legend.title = element_blank())+
  theme(legend.text = element_text(face = "bold"))

Animating the graph with ggplotly

Click “Play” below to watch the life expectancies change over time.

library(plotly)
library(dplyr)

p <-ggplot(WorldBankData,
       aes(x=year,
           y=life_expectancy,
           color=country,
           frame=year))+
  geom_point(size=3)+
  labs(title = "Life Expectancy Over Time",
       x="Year",
       y="Life Expectancy")+
theme(plot.title = element_text(hjust = 0.5, face = "bold", size = 14))+
theme(axis.title.x = element_text(hjust=0.5, face = "bold"))+
  theme(axis.title.y = element_text(face = "bold"))+
  theme(legend.position = "bottom")+
  theme(legend.title = element_blank())+
  theme(legend.text = element_text(face = "bold"))

ggplotly(p)